package com.tamecode.bean.factory;

import com.tamecode.bean.di.Car;
import org.springframework.beans.factory.FactoryBean;

import java.math.BigDecimal;

public class CarFactoryBean implements FactoryBean<Car> {

    private String carInfo;

    public String getCarInfo() {
        return carInfo;
    }

    // 1. 接收逗号分隔的属性设置信息
    public void setCarInfo(String carInfo) {
        this.carInfo = carInfo;
    }

    // 2. 实例化 car Bean，如果 isSingleton 返回 true，就会把对象放到spring的单实例缓存池中
    @Override
    public Car getObject() throws Exception {
        Car car = new Car();
        String[] infos = carInfo.split(",");
        car.setBrand(infos[0]);
        car.setMaxSpeed(Integer.parseInt(infos[1]));
        car.setPrice(new BigDecimal(Double.parseDouble(infos[2])));
        return car;
    }

    // 3. 返回 Car 的类型
    @Override
    public Class<?> getObjectType() {
        return Car.class;
    }

    // 4. 标识通过该 FActoryBean 返回的 Bean 是 singleton
    @Override
    public boolean isSingleton() {
        return false;
    }
}
